
import queue
import threading
import time


def plc_write16(t, x):
    #print('plc_write16', t, x)
    return 1

def plc_read16(t):
    #print('plc_write16', t, x)
    return 1

def get_cam_ready(camid):
    return 1


def plc_out(isok0, isok1, pos):
    return 0

def plc_out_pos(pos):
    # 检测结果
    print(f'plc_out_pos {pos}')

def plc_out_ng(isok0, isok1):
    # 检测结果
    PLC_OK = ["D1020", "D1022"]
    print(f'plc_out_ng {isok0} {isok1}')

def plc_work_thd(ss):
    print("进入工作状态")
    # 告知PLC CAM处于READY状态可以接受数据
    while ss.running:
        ss.flag = 1
        ss.max_code = 0
        ss.qq.put(1)
        for i in range(1000):
            ss.code = i*5
            ss.max_code = ss.code
            time.sleep(0.01)
        if not ss.running:
            break
        #ss.qq.put(0)
        ss.flag = 0
        time.sleep(1)
    pass

time000 = time.time()
def plc_get_code():
    global time000
    time00011 = time.time()
    code = int((time00011-time000)*1000)
    if code>7000:
        time000 = time00011
    return code
# 定时不断发送心跳信号，告知PLC

def plc_in_saoma():
    t1 = int(time.time())%10
    return 1 if t1>3 else 0


class MyMcPlc:

    def ConnectServer(self):
        return 1

    def CloseSocket(self):
        return 0

melsecr = MyMcPlc()
melsecw = MyMcPlc()


def plc_set_ng(end):
    if end:
        plc_write16('D30', 1)
    else:
        plc_write16('D30', 2)

def plc_obj_is_in():
    #检测结果
    t = 'D60' #上升沿
    b = plc_read16(t)

    if b is not None and b>0:
        print('上升', b)
        time.sleep(0.2)
        plc_write16(t, 0)
        #print(b)
    return b

def plc_obj_is_out():
    #检测结果
    t = 'D62'  #下降沿
    b = plc_read16(t)
    if b is not None and b>0:
        time.sleep(0.2)
        plc_write16(t, 0)
        #print(b)
    return b

def plc_set_ng(end):
    if end:
        plc_write16('D30', 1)
    else:
        plc_write16('D30', 2)

pre_live_time = time.time()
def plc_live():
    t = time.time()
    global pre_live_time
    tt = t-pre_live_time
    if tt>4:
        plc_write16('D31', 1)
        pre_live_time = t


def test_myplc():
    plc = PlcSL()
    plc.start()
    for i in range(10):
        x = plc.qq.get()
        print('plc', x, plc.flag)
    plc.stop()
    print('asdf')


if __name__ == '__main__':
    test_myplc()
    pass
